package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

// 2.7.8
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private  CheckGroupService checkGroupService;

    @Autowired
    private  CheckItemService checkItemService;


    @Override
    public boolean saveSetmeal(SetmealDTO setmealDTO)  {
        //  如果用户  超时提交  提示用户 重新上传图片信息
        if(RedisUtil.existsKey(RedisConstant.SINGLE_PIC + setmealDTO.getImg())==false){
            throw  new RuntimeException("超时，请重新提交图片！");
        }
        //  套餐录入  中间表数据  循环录入
        boolean save = save(setmealDTO);
        Integer sid = setmealDTO.getId();
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        if(checkgroupIds!=null){
            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetmealAndGroupInfo(sid,checkgroupId);
            }
        }

        //  Set  集合 移除指定 图片
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        return save;
    }

// 项目实战 ：  逻辑 删除    编辑
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageResult = null;
        if(StringUtils.isNotBlank(queryPageBean.getQueryString())){
            //  将分页查询结果集  封装到 自定义的分页结果集
            QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<Setmeal>();
            queryWrapper
                    .like("name",queryPageBean.getQueryString()).
                    or().
                    like("HELPCODE",queryPageBean.getQueryString())
                    .or()
                    .like("code",queryPageBean.getQueryString());
             //  where  name like  xxx  or  helpcode  like  xxx
            pageResult = page(new Page<Setmeal>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }else{
            //  无条件的分页查询 业务层实现
            pageResult = page(new Page<Setmeal>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()));
        }
        return new PageResult(pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public void clearImg() {
        //   清理 Set  集合里面垃圾图片  该方法  任务调度 来调用该业务实现。。
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        for (String uuidfilename : members) {
           String  existFilename = RedisConstant.SINGLE_PIC+uuidfilename;
            if(RedisUtil.existsKey(existFilename)==false){
                 //  true   可以 存在  没有过期    false  过期   oss  删除
                AliyunUtils.deleteFile(uuidfilename);
                RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
                System.out.println("----垃圾图片清理了一次----"+uuidfilename);
            }
        }

    }

    /**
     * 条件 套餐id
     *    内连接查询    外连接     left/right   join  on  关键字
     *     集合 ：10  驱动表    *   参照表 100   =1000  直积   笛卡尔积过滤
     *
      */
    @Override
    public SetmealVO findSetMealDetail(int id) {
        //   多表联查   完成 5表数据   封装  SetmealVO对象 + 代码  ： 项目实战：  redis    aop
        SetmealVO  setmealVO  = baseMapper.findSetmealById(id);

        List<CheckGroupVO>  checkGroupVOList = checkGroupService.findCheckGroupInfoBySetmealId(id);

        for (CheckGroupVO checkGroupVO : checkGroupVOList) {

            List<CheckItem> checkItemList = checkItemService.findCheckItemByGroupId(checkGroupVO.getId());

            checkGroupVO.setCheckItems(checkItemList);
        }
        setmealVO.setCheckGroupVOList(checkGroupVOList);
        return setmealVO;
    }

    @Override
    public Setmeal findSetmealById(int id) {
        return baseMapper.selectById(id);
    }


}
